package cn.ad.server.web.service;

import cn.ad.server.domain.ad.AmountStore;
import cn.ad.server.domain.ad.Campaign;
import cn.ad.server.domain.ad.Strategy;
import cn.ad.server.persistence.AmountStoreRepository;
import cn.ad.server.persistence.CampaignRepository;
import cn.ad.server.persistence.StrategyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 广告订单处理服务类，提供订单相关数据处理
 * @author Wu Chengjun
 *
 */
@Service
public class CampaignService {

	@Autowired
	private AmountStoreRepository amountStoreRepository;

	@Autowired
	private CampaignRepository campaignRepository;

	@Autowired
	private StrategyRepository strategyRepository;

	@Transactional
	public void saveOrUpdateStrategyAmountStore(Strategy s) {
		List<AmountStore> ass = strategyRepository.findAmounts(s);
		for(AmountStore as : ass) {
			amountStoreRepository.saveOrUpdate(as);
		}
		
	}

	@Transactional
	public void saveOrUpdateCampaignAmountSotre(Campaign c) {
		List<AmountStore> ass = campaignRepository.findAmounts(c);
		for(AmountStore as : ass) {
			amountStoreRepository.saveOrUpdate(as);
		}
	}

	public List<Campaign> findAvailableCampaign() {
		return campaignRepository.findAvailable();
	}

}
